import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dz.glory.common.jedis.client.JedisClient;
import com.dz.glory.common.jedis.support.Key;
import com.dz.glory.common.tools.support.CallBack;
import com.dz.glory.common.utils.RandomUtil;
import com.dz.glory.common.vo.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * ${table.comment} 服务类 实现类
 * </p>
 *
 * @author ${author}
 * @since ${date?date}
 */
@Service
public class ${table.serviceImplName} extends ServiceImpl<${table.mapperName}, ${entity}> implements ${entity}Service {

    @Resource
    private ${table.mapperName} ${table.entityPath}Mapper;

    @Resource
    private JedisClient jedisClient;

    private static final String KEY_VERSION = "v1";

    @Override
    public Page<${entity}VO> getList(${entity}Req req) {
        return MPPageUtils.convert(${table.entityPath}Mapper.getList(req), ${entity}VO.class);
    }

    @Override
    public ${entity}VO getById(Integer id) {
        return MPPageUtils.convert(super.getById(id), ${entity}VO.class);
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<${entity}VO> getAll() {
        return jedisClusterClient.getCache(new Key(ICacheKey.${table.name?upper_case}, EnvUtil.getEnv(), KEY_VERSION), RandomUtil.getCacheTemp(), new CallBack<List<${entity}VO>>() {
            @Override
            public List<${entity}VO> call() {
                LambdaQueryWrapper<${entity}> queryWrapper = new LambdaQueryWrapper<>();
                <#list table.fields as field>
                <#if field.propertyName == "status">
                queryWrapper.eq(${entity}::getStatus, 1);
                </#if>
                </#list>
                <#list table.fields as field>
                <#if field.propertyName == "sort">
                queryWrapper.orderByAsc(${entity}::getSort);
                </#if>
                </#list>
                queryWrapper.orderByDesc(${entity}::getId);
                List<${entity}> ${table.entityPath}s = ${table.entityPath}Mapper.selectList(queryWrapper);
                return MPPageUtils.convert(${table.entityPath}s, ${entity}VO.class);
            }
        });
    }

    @Override
    public ${entity}VO saveOrUpdate(${entity}VO vo) {
        ${entity} ${table.entityPath} = MPPageUtils.convert(vo, ${entity}.class);
        super.saveOrUpdate(${table.entityPath});
        clearCache();
        return vo;
    }

    @Override
    public Boolean delete(List<Integer> ids) {
        super.removeByIds(ids);
        clearCache();
        return true;
    }

<#list table.fields as field>
    <#if field.propertyName == "sort">
    @Override
    public Integer updateSort(${entity}Req req) {
        int result = ${table.entityPath}Mapper.updateSort(req);
        if (result == 1) {
            clearCache();
        }
        return result;
    }
    </#if>
    <#if field.propertyName == "status">
    @Override
    public Integer updateStatus(${entity}Req req) {
        int result = ${table.entityPath}Mapper.updateStatus(req);
        if (result == 1) {
            clearCache();
        }
        return result;
    }
    </#if>
</#list>

    public void clearCache() {
        jedisClient.del(DB_2, new Key(ICacheKey.${table.name?upper_case}, EnvUtil.getEnv(), KEY_VERSION));
    }
}
